using System.Linq;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(Trigger))]
public class TriggerEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        var t = target as Trigger;
        if (!t)
        {
            EditorGUILayout.HelpBox("Unexpected Null Reference.", MessageType.Error);
            return;
        }

        t.targetObject = EditorGUILayout.ObjectField("Target", t.targetObject, typeof(Transform), true) as Transform;
        t.Invokable =
            EditorGUILayout.ObjectField("Invokable Object", t.Invokable, typeof(TriggerInvokable), true) as
                TriggerInvokable;
        Trigger.UpdateTriggerReference();
        if (!t.GetComponent<Collider>() || !t.GetComponent<Collider>().isTrigger)
        {
            EditorGUILayout.HelpBox("No collider is found attached to this Game Object or it is not a trigger.",
                MessageType.Warning);
        }
        else if (t.GetComponents<Collider>().Count(collider => collider.isTrigger) > 1)
        {
            EditorGUILayout.HelpBox("More than one triggers are found attached to this Game Object.",
                MessageType.Warning);
        }
        else
        {
            EditorGUI.BeginDisabledGroup(true);
            EditorGUILayout.ObjectField("Trigger Attached",
                t.GetComponents<Collider>().First(collider => collider.isTrigger), typeof(Collider), true);
            EditorGUI.EndDisabledGroup();
        }
    }
}